package com.mmlc.meimei.lichengkongjian.home;

import android.os.Handler;
import android.os.Message;
import android.view.View;

import com.androidnetframe.BaseParentActivity;
import com.androidnetframe.httputil.BaseRequestFilterLayer;
import com.androidnetframe.responsejavabean.RequestError;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.mmlc.inter.InterFragment;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.network.filter.ActiveInfoListRequestFilter;
import com.mmlc.network.responsebean.ActiveInfoListResponseBean;
import com.mmlc.utils.ToastUtils;
import com.mmlc.utils.ViewUtils;

import java.util.ArrayList;

/**
 * description: 大V推荐
 * by king
 * Created at 2016/8/26 11:27
 */
public class RecommendFragment extends InterFragment {

    @Override
    protected int initResourceID() {
        return R.layout.fragment_fashion;
    }

    PullToRefreshListView refresh_list;
    ArrayList<ActiveInfoListResponseBean.DataBean> datas;
    FashionRecommendAdapter adapter;

    @Override
    protected void initView() {
        refresh_list = ViewUtils.getView(view, R.id.refresh_list);
        datas = new ArrayList<>();
        adapter = new FashionRecommendAdapter(context, datas);
        refresh_list.setAdapter(adapter);
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        refresh_list.setMode(PullToRefreshBase.Mode.DISABLED);
    }

    int pageNow = 1;
    boolean isOk = true;

    @Override
    protected void initInfo() {
        super.initInfo();
        isOk = false;

        ActiveInfoListRequestFilter filter = new ActiveInfoListRequestFilter((BaseParentActivity) getActivity());
        filter.requestBean.cate_id=getArguments().getString("id");
        filter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<ActiveInfoListResponseBean>() {
            @Override
            public void onSuccess(ActiveInfoListResponseBean result) {
                loadComplete();
                if (pageNow == 1) datas.clear();
                datas.addAll(result.getData());
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onFailed(View errorLayout, RequestError serverError, int failedType) {
                loadComplete();
                ToastUtils.showToast(serverError.mess);
            }
        });
    }

    private void loadComplete() {
        isOk = true;
        refreshHandler.sendEmptyMessage(0);
    }

    Handler refreshHandler = new Handler() {
        @Override
        public void dispatchMessage(Message msg) {
            super.dispatchMessage(msg);
            if (msg.what == 0) {
                if (!isOk) {
                    refresh_list.onRefreshComplete();
                    isOk = true;
                }
            }
        }
    };

}
